package com.chart.web.controller.chart.sta;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.domian.ChartData;
import com.chart.chart.domian.ChartLegendData;
import com.chart.chart.domian.vo.ChartXData;
import com.chart.chart.domian.vo.ChartXDataVo;
import com.chart.chart.service.IChartConfigsService;
import com.chart.common.core.controller.BaseController;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.utils.ExcelDataUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * 折线图
 *
 * @author chart
 */
@RestController
@RequestMapping("/line/chart")
public class LineChartController extends BaseController
{


    @Autowired
    private IChartConfigsService chartConfigsService;

    //多线条圆滑折线图
    @PostMapping("/multiLineSmoothLine/importData")
    public AjaxResult multiLineSmoothLine(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<List<Object>> dataList = new ArrayList<>();
            for (int i = 0; i < getdata.size(); i++) {
                List<String> rowData = getdata.get(i);
                ArrayList<String> newList = new ArrayList<>();
                for (int i1 = 0; i1 < rowData.size(); i1++) {
                    String s = rowData.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        newList.add(s);
                    }
                }
                if (!CollectionUtils.isEmpty(newList)) {
                if (i == 0) {
                    List<Object> lists = new ArrayList<>();
                    lists.addAll(newList);
                    dataList.add(lists);
                } else {
                        List<Object> singleData = new ArrayList<>();
                        for (int i1 = 0; i1 < newList.size(); i1++) {
                            if (i1==0) {
                                String s = newList.get(i1);
                                if (!ObjectUtils.isEmpty(s)) {
                                    singleData.add(newList.get(i1));
                                }else {
                                    singleData.add(0);
                                }
                            }else {
                                    singleData.add(Double.parseDouble(newList.get(i1)));
                                }
                            }
                        dataList.add(singleData);
                        }
                    }
                }


            ChartXDataVo xData = new ChartXDataVo();
            xData.setSource(dataList);


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            jsonObject.put("dataset", xData);

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject o = series.get(1);
            List<JSONObject> seriesList = new ArrayList<>();
            for (int i = 1; i < getdata.get(0).size(); i++) {
                JSONObject o1 = new JSONObject();
                o1.putAll(o);
                o1.put("name", getdata.get(0).get(i));
                seriesList.add(o1);
            }
            jsonObject.put("series", seriesList);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            List<String> fistName = getdata.get(0);
            fistName.remove(0);
            List<ChartLegendData> chartLegendData = new ArrayList<>();
            for (int i = 0; i < fistName.size(); i++) {
                ChartLegendData data = new ChartLegendData();
                data.setName(fistName.get(i));
                chartLegendData.add(data);
            }
            legend.put("data", chartLegendData);
            jsonObject.put("legend", legend);


            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }


    //心电图
    @PostMapping("/electrocardiogramCurve/importData")
    public AjaxResult electrocardiogramCurve(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xdata =new ArrayList<>();
            List<String> xlist = getdata.get(0);
            for (int i = 1; i < xlist.size(); i++) {
                String s = xlist.get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    xdata.add(s);
                }
            }
//        xdata.remove(0);

            List<ChartXDataVo> chartXDataVos = new ArrayList<>();
            List<String> ydata = new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<List<Object>>  groupList= new ArrayList<>();
                ChartXDataVo sData = new ChartXDataVo();
                List<String> list = getdata.get(i);
                for (int i1 = 0; i1 < list.size(); i1++) {
                    String s = list.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        if (i1==0) {
                            ydata.add(s);
                        }else {
                            List<Object> cellData = new ArrayList<>();
                            cellData.add(xdata.get(i1-1));
                            cellData.add(Double.parseDouble(s));
                            groupList.add(cellData);
                        }
                    }
                }
                sData.setSource(groupList);
                chartXDataVos.add(sData);
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            jsonObject.put("dataset", chartXDataVos);

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(1);

            ArrayList<JSONObject> jsonObjects = new ArrayList<>();
            for (int i = 0; i < ydata.size() ; i++) {
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(jsonObject1);
                jsonObject2.put("datasetIndex", i);
                jsonObject2.put("name", ydata.get(i));
                jsonObjects.add(jsonObject2);
            }
            jsonObject.put("series", jsonObjects);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
//            xdata.remove(0);
            List<ChartLegendData> chartLegendData = new ArrayList<>();
            for (int i = 0; i < ydata.size(); i++) {
                ChartLegendData data = new ChartLegendData();
                data.setName(ydata.get(i));
                chartLegendData.add(data);
            }
            legend.put("data", chartLegendData);
            jsonObject.put("legend", legend);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }


    //多层圆滑折线图
    @PostMapping("/multiLayerSmoothLine/importData")
    public AjaxResult multiLayerSmoothLine(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }
            List<String> fistName = new ArrayList<>();
            List<String> data = getdata.get(0);
            for (int i = 0; i < data.size(); i++) {
                String s = data.get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    fistName.add(s);
                }
            }

            List<List<String>> newList = new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);
                if (!CollectionUtils.isEmpty(list)) {
                    List<String> list1 = new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        String s = list.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            list1.add(s);
                        }
                    }
                    newList.add(list1);
                }
            }

            Map<String, List<List<String>>> map = new HashMap<>();
            newList.add(0,fistName);
            map.put("source",newList);
            ArrayList<Map> maps = new ArrayList<>();
            maps.add(map);

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

            jsonObject.put("dataset",maps);

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject json = series.get(0);

            List<JSONObject> jsonObjects = new ArrayList<>();
            ArrayList<Map> namesMap = new ArrayList<>();
            for (int i = 1; i < fistName.size(); i++) {
                String s = fistName.get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.putAll(json);

                    jsonObject1.put("name",s);
                    HashMap<String, String> nameMap = new HashMap<>();
                    nameMap.put("name",s);
                    namesMap.add(nameMap);
                    jsonObjects.add(jsonObject1);
                }
            }
            jsonObject.put("series",jsonObjects);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            legend.put("data",namesMap);
            jsonObject.put("legend",legend);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }


    //河流图
    @PostMapping("/riverMap/importData")
    public AjaxResult riverMap(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> fistName = getdata.get(0);

            ChartXData xDatas = new ChartXData();

            List<String> strings = getdata.get(1);
            String s = strings.get(3);

            List<List<String>> list = new ArrayList<>();
            for (int i = 0; i < getdata.size(); i++) {
                List<String> list1 = getdata.get(i);
                if (!CollectionUtils.isEmpty(list1)) {
                    List<String> list2 = new ArrayList<>();
                    for (int i1 = 0; i1 < list1.size(); i1++) {
                        String s1 = list1.get(i1);
                        if (!ObjectUtils.isEmpty(s1) && !s1.equals("")) {
                            list2.add(s1);
                        }
                    }
                    if (i == 0) {
                        list.add(list2);
                    } else {
                        list.add(list2);
                    }
                }
            }

            xDatas.setSource(list);


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            List<JSONObject> sDatas = new ArrayList<>();


            for (Integer integer = 1; integer < fistName.size(); integer++) {
                JSONObject jsonObject1 = series.get(integer - 1);
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(jsonObject1);
                JSONObject endLabel = (JSONObject) jsonObject2.get("endLabel");
                endLabel.put("formatter",fistName.get(integer));
                jsonObject2.put("endLabel",endLabel);

                JSONObject areaStyle = (JSONObject) jsonObject2.get("areaStyle");
                areaStyle.put("origin",s);
                sDatas.add(jsonObject2);
            }


            jsonObject.put("dataset", xDatas);
            jsonObject.put("series", sDatas);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }


    //面板ECDF图
    @PostMapping("/panelECDF/importData")
    public AjaxResult importData(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }


            List<String> xdata = getdata.get(0);

            ArrayList<ChartXDataVo> chartXDataVos = new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<List<Object>> groupList = new ArrayList<>();
                ChartXDataVo sData = new ChartXDataVo();
                List<String> colData = getdata.get(i);
                for (int i1 = 1; i1 < colData.size(); i1++) {
                    List<Object> cellData = new ArrayList<>();
                    cellData.add(xdata.get(i1));
                    cellData.add(Double.parseDouble(colData.get(i1)));
                    groupList.add(cellData);
                }
                sData.setSource(groupList);
                chartXDataVos.add(sData);
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            jsonObject.put("dataset", chartXDataVos);

            int size = getdata.size();
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject o = series.get(1);
            List<JSONObject> seriesList = new ArrayList<>();
            for (int i = 1; i < size; i++) {
                JSONObject o1 = new JSONObject();
                o1.putAll(o);
                o1.put("name", getdata.get(i).get(0));
                o1.put("datasetIndex", i - 1);
                seriesList.add(o1);
            }
            jsonObject.put("series", seriesList);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            List<ChartLegendData> chartLegendData = new ArrayList<>();
            for (int i = 1; i < size; i++) {
                ChartLegendData data = new ChartLegendData();
                data.setName(getdata.get(i).get(0));
                chartLegendData.add(data);
            }
            legend.put("data", chartLegendData);
            jsonObject.put("legend", legend);


            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }


    //单一折线图
    @PostMapping("/basic/importData")
    public AjaxResult basic(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<Map> roomName = new ArrayList<>();


            List<String> strings = getdata.get(1);
            HashMap<String, String> map = new HashMap<>();
            map.put("name", strings.get(0));
            roomName.add(map);

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            legend.put("data",roomName);

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");

            List<JSONObject> list = new ArrayList<>();
            for (int i = 0; i < series.size(); i++) {
                JSONObject o1 = series.get(i);
                o1.put("name",roomName.get(i).get("name"));
                List<String> list1 =getdata.get(i+1);
                ArrayList<String> newList = new ArrayList<>();
                for (int i1 = 1; i1 < list1.size(); i1++) {
                    String s = list1.get(i1);
                    if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                        newList.add(s);
                    }
                }
                o1.put("data",newList);
                list.add(o1);
            }

            JSONObject xAxis = (JSONObject) jsonObject.get("xAxis");
            List<String> strings1 = getdata.get(0);
            strings1.remove(0);

            xAxis.put("data",strings1);

            jsonObject.put("xAxis",xAxis);
            jsonObject.put("legend",legend);
            jsonObject.put("series",list);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }


    //阶梯折线图
    @PostMapping("/stepLine/importData")
    public AjaxResult stepLine(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xlist = new ArrayList<>();
            List<String> xdata = getdata.get(0);
            for (int i = 1; i < xdata.size(); i++) {
                String s = xdata.get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    xlist.add(s);
                }
            }

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());



            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject json = series.get(0);

            List<Map> maps = new ArrayList<>();
            List<JSONObject> jsonObjects = new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);
                if (!CollectionUtils.isEmpty(list)) {
                    ArrayList<String> newList = new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        String s = list.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            newList.add(s);
                        }
                    }
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.putAll(json);
                    jsonObject1.put("name",newList.get(0));
                    Map<String, String> map = new HashMap<>();
                    map.put("name",newList.get(0));
                    maps.add(map);
                    newList.remove(0);
                    jsonObject1.put("data",newList);
                    jsonObjects.add(jsonObject1);
                }
            }

            JSONObject xAxis = (JSONObject) jsonObject.get("xAxis");
            xAxis.put("data",xlist);
            jsonObject.put("xAxis",xAxis);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            legend.put("data",maps);
            jsonObject.put("legend",legend);

            jsonObject.put("series",jsonObjects);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }

    @PostMapping("/doublex/importData")
    public AjaxResult doublex(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> groupName = new ArrayList<>();
            List<String> x1 = new ArrayList<>();
            List<String> x2 = new ArrayList<>();

            List<String> y1 = new ArrayList<>();
            List<String> y2 = new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);
                if (!CollectionUtils.isEmpty(list)) {
                    List<String> data = new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        String s = list.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            data.add(s);
                        }
                    }

                    if (data.size()==1) {
                        groupName.add(data.get(0));
                    }else {
                        if (groupName.size()==1) {
                            for (int i1 = 0; i1 < data.size(); i1++) {
                                String s = data.get(i1);
                                if (i1==0) {
                                    x1.add(s);
                                }else {
                                    y1.add(s);
                                }
                            }
                        }else {
                            for (int i1 = 0; i1 < data.size(); i1++) {
                                String s = data.get(i1);
                                if (i1==0) {
                                    x2.add(s);
                                }else {
                                    y2.add(s);
                                }
                            }
                        }
                    }
                }
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            List<Map> maps = new ArrayList<>();
            for (int i = 0; i < groupName.size(); i++) {
                Map map = new HashMap<>();
                map.put("name",groupName.get(i));
                maps.add(map);
            }

            legend.put("data",maps);
            jsonObject.put("legend",legend);

            List<JSONObject> xAxis = (List<JSONObject>) jsonObject.get("xAxis");
            for (int i = 0; i < xAxis.size(); i++) {
                JSONObject jsonObject1 = xAxis.get(i);
                if (i==0) {
                    jsonObject1.put("data",x1);
                }else {
                    jsonObject1.put("data",x2);
                }
            }
            jsonObject.put("xAxis",xAxis);

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            for (int i = 0; i < series.size(); i++) {
                JSONObject jsonObject1 = series.get(i);
                jsonObject1.put("name",groupName.get(i));
                if (i==0) {
                    jsonObject1.put("data",y1);
                }else {
                    jsonObject1.put("data",y2);
                }
            }

            jsonObject.put("series",series);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }

    @PostMapping("/doubley/importData")
    public AjaxResult doubley(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xdata = new ArrayList<>();
            List<String> names = new ArrayList<>();
            List<List<String>> strings = new ArrayList<>();
            for (int i = 0; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);
                List<String> datas =new ArrayList<>();
                for (int i1 = 0; i1 < list.size(); i1++) {
                    String s = list.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        if (i1==0) {
                            names.add(s);
                        }else {
                            datas.add(s);
                        }
                    }
                }
                if (i==0) {
                    xdata.addAll(datas);
                }else {
                    strings.add(datas);
                }
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            List<JSONObject> yAxis = (List<JSONObject>) jsonObject.get("yAxis");
            for (int i = 0; i < series.size(); i++) {
                if (i==0) {
                    JSONObject jsonObject1 = series.get(i);
                    jsonObject1.put("name",names.get(i+1));
                    List<String> list = strings.get(i);
                    //求出最大值（绝对值）并返回把最大值最高位+1的数值
                    int max = ExcelDataUtils.Max(list);
                    int min = ExcelDataUtils.Min(list);
                    JSONObject jsonObject2 = yAxis.get(i);
                    jsonObject2.put("max",max);
                    jsonObject2.put("min",min);
                    jsonObject2.put("name",names.get(i+1));
                    List<Map> maps = new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        double s = Double.parseDouble(list.get(i1));
                        Map map = new HashMap<>();
                        map.put("value",s);
                        if (s>0) {
                            map.put("itemStyle",rightColor);
                        }else {
                            map.put("itemStyle",leftColor);
                        }
                        maps.add(map);
                    }
                    jsonObject1.put("data",maps);
                }else {
                    JSONObject jsonObject1 = series.get(i);
                    jsonObject1.put("name",names.get(i+1));
                    List<String> list = strings.get(i);
                    int max = ExcelDataUtils.Max(list);
                    int min = ExcelDataUtils.Min(list);
                    JSONObject jsonObject2 = yAxis.get(i);
                    jsonObject2.put("max",max);
                    jsonObject2.put("min",min);
                    jsonObject2.put("name",names.get(i+1));
                    jsonObject1.put("data",list);
                }
            }

            List<Map> maps1 = new ArrayList<>();
            for (int i = 1; i < names.size(); i++) {
                Map map = new HashMap<>();
                map.put("name",names.get(i));
                maps1.add(map);
            }

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            legend.put("data",maps1);
            jsonObject.put("legend",legend);

            JSONObject xAxis = (JSONObject) jsonObject.get("xAxis");
//            xdata.remove(0);
            xAxis.put("data",xdata);
            jsonObject.put("xAxis",xAxis);


            jsonObject.put("series",series);
            jsonObject.put("yAxis",yAxis);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }

    //堆叠折线图
    @PostMapping("/stackedLine/importData")
    public AjaxResult stackedLine(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xdata =new ArrayList<>();
            List<String> xlist = getdata.get(0);
            for (int i = 1; i < xlist.size(); i++) {
                String s = xlist.get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    xdata.add(s);
                }
            }
//        xdata.remove(0);

            List<ChartXDataVo> chartXDataVos = new ArrayList<>();
            List<String> ydata = new ArrayList<>();
            List<List<Object>>  groupList= new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);

                List<Object> cellData = new ArrayList<>();
                for (int i1 = 0; i1 < list.size(); i1++) {
                    String s = list.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        if (i1==0) {
                            ydata.add(s);
                        }else {
                            cellData.add(Double.parseDouble(s));
                        }
                    }
                }
                groupList.add(cellData);
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            jsonObject.put("dataset", chartXDataVos);

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(1);

            ArrayList<JSONObject> jsonObjects = new ArrayList<>();
            for (int i = 0; i < ydata.size() ; i++) {
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(jsonObject1);
                jsonObject2.put("data", groupList.get(i));
                jsonObject2.put("name", ydata.get(i));
                jsonObjects.add(jsonObject2);
            }
            jsonObject.put("series", jsonObjects);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
//            xdata.remove(0);
            List<ChartLegendData> chartLegendData = new ArrayList<>();
            for (int i = 0; i < ydata.size(); i++) {
                ChartLegendData data = new ChartLegendData();
                data.setName(ydata.get(i));
                chartLegendData.add(data);
            }
            legend.put("data", chartLegendData);
            jsonObject.put("legend", legend);

            JSONObject xAxis = (JSONObject) jsonObject.get("xAxis");
            xAxis.put("data",xlist);
            jsonObject.put("xAxis", xAxis);


            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }



    //图表信息的颜色配置
    private static Map rightColor=new HashMap();
    static {
        rightColor.put("type","linear");
        rightColor.put("x",0);
        rightColor.put("y",0);
        rightColor.put("x2",0);
        rightColor.put("y2",1);
        List<Map> maps = new ArrayList<>();
        Map map1 = new HashMap<>();
        map1.put("offset",0);
        map1.put("color","#ff9569");
        maps.add(map1);
        Map map2 = new HashMap<>();
        map2.put("offset",1);
        map2.put("color","#e92758");
        maps.add(map2);
        rightColor.put("colorStops",maps);
        rightColor.put("global",false);

        List<Integer> list = new ArrayList<>();
        list.add(30);
        list.add(30);
        list.add(0);
        list.add(0);
        rightColor.put("barBorderRadius",list);
    }

    private static Map leftColor=new HashMap();
    static {
        leftColor.put("type","linear");
        leftColor.put("x",0);
        leftColor.put("y",0);
        leftColor.put("x2",0);
        leftColor.put("y2",1);
        List<Map> maps = new ArrayList<>();
        Map map1 = new HashMap<>();
        map1.put("offset",0);
        map1.put("color","#e92758");
        maps.add(map1);
        Map map2 = new HashMap<>();
        map2.put("offset",1);
        map2.put("color","#ff9569");
        maps.add(map2);
        leftColor.put("colorStops",maps);
        leftColor.put("global",false);

        List<Integer> list = new ArrayList<>();
        list.add(0);
        list.add(0);
        list.add(30);
        list.add(30);
        leftColor.put("barBorderRadius",list);
    }

}
